package com.whli.jee.system.service.impl;

import com.whli.jee.common.web.dao.IBaseDao;
import com.whli.jee.common.web.service.impl.BaseServiceImpl;
import com.whli.jee.system.dao.ISysUserRoleDao;
import com.whli.jee.system.entity.SysUserRole;
import com.whli.jee.system.service.ISysUserRoleService;
import com.whli.jee.system.vo.SysUserRoleVO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author whli
 * @version 1.0
 * @since 1.0
 */
@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends BaseServiceImpl<SysUserRole> implements ISysUserRoleService {

    @Autowired
    private ISysUserRoleDao sysUserRoleDao;

    @Override
    public IBaseDao<SysUserRole> getDao() {
        return sysUserRoleDao;
    }


    @Override
    public List<String> listByUser(String userId) {
        return sysUserRoleDao.listByUser(userId);
    }

    @Override
    public int grant(SysUserRoleVO entity) {
        sysUserRoleDao.deleteRolesByUser(entity.getUserKey(),entity.getOfficeId());

        if (CollectionUtils.isNotEmpty(entity.getRoleKeys())){
            List<SysUserRole> userRoles = new ArrayList<>();
            for (String roleKey : entity.getRoleKeys()){
                SysUserRole userRole = new SysUserRole();
                userRole.setUserId(entity.getUserKey());
                userRole.setRoleId(roleKey);
                userRoles.add(userRole);
            }
            return saveBatch(userRoles);
        }
        return 1;
    }


}
